import java.util.Arrays;
import java.util.Stack;

public class Sort {
//    public static void instersort(int[] arr){
//        for (int i = 1; i<arr.length ; i++) {
//            int tmp=arr[i];
//            int j=i-1;
//            for (;j>=0 ;j--) {
//                if(arr[j]>tmp){
//                    arr[j+1]=arr[j];
//                }else{
//                    break;
//                }
//            }
//           arr[j+1]=tmp;
//        }
//    }

//    public static void instersort(int[] arr){
//        for (int i = 1; i <arr.length ; i++) {
//            int tmp=arr[i];
//            int j = i-1;
//            for ( ;j >=0 ; j--) {
//                if(arr[j]>=tmp){
//                    arr[j+1]=arr[j];
//                }else{
//                    break;
//                }
//
//            }
//           arr[j+1]=tmp;
//        }
//    }

    public static void instersort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }

            }
            arr[j + 1] = tmp;
        }
    }

    public static void sellsort(int[] arr) {
        int gop = arr.length;
        while (gop > 1) {
            gop /= 2;
            sell(arr, gop);
        }
    }

    public static void sell(int[] arr, int gap) {
        for (int i = gap; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (arr[j] > tmp) {
                    arr[j + gap] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + gap] = tmp;
        }
    }

    public static void selecisort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int min = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[min]) {
                    min = j;
                }
            }
            swap(arr, i, min);
        }
    }

    public static void swap(int[] arr, int x, int y) {
        int tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }

    public static void selecisort2(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int min = left;
            int max = left;
            for (int i = left + 1; i <= right; i++) {
                if (arr[min] > arr[i]) {
                    min = i;
                }
                if (arr[max] < arr[i]) {
                    max = i;
                }
            }
            swap(arr, left, min);
            if (left == min) {
                max = min;
            }
            swap(arr, right, max);
            left++;
            right--;
        }

    }

    private void createbigheap(int[] arr) {
        for (int i = (arr.length - 1 - 1) / 2; i >= 0; i--) {
            shiftdown(arr, i, arr.length);
        }
    }

    private void shiftdown(int[] arr, int parent, int len) {
        int child = 2 * parent + 1;
        while (child < len) {
            if (child + 1 < len && arr[child + 1] > arr[child]) {
                child++;
            }
            if (arr[child] > arr[parent]) {
                swap(arr, child, parent);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    public void heapSort(int[] arr) {
        createbigheap(arr);
        int end = arr.length - 1;
        while (end > 0) {
            swap(arr, 0, end);
            shiftdown(arr, 0, end);
            end--;
        }

    }

    public void bubblesort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                    flag = true;
                }
            }
            if (flag != true) {
                break;
            }
        }
    }

    public void quicksort(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        quick(arr, left, right);
    }

    public void quick(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int tmp = partition(arr, left, right);
        quick(arr, left, tmp - 1);
        quick(arr, tmp + 1, right);
    }

    public int partition(int[] arr, int left, int right) {
        int tmp = arr[left];
        int i = left;
        while (left < right) {
            while (left < right && arr[right] >= tmp) {
                right--;
            }
            while (left < right && arr[left] <= tmp) {
                left++;
            }
            swap(arr, left, right);
        }
        swap(arr, left, i);
        return left;
    }

    public int partition2(int[] arr, int left, int right) {
        int tmp = arr[left];
        while (left < right) {
            while (left < right && arr[right] >= tmp) {
                right--;
            }
            arr[left] = arr[right];
            while (left < right && arr[left] <= tmp) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = tmp;
        return left;
    }

    //前后指针法
    public int partition3(int[] arr, int left, int right) {
        int prev = left;
        int cur = left + 1;
        while (cur <= right) {
            if (arr[cur] < arr[left] && arr[++prev] != arr[cur]) {
                swap(arr, prev, cur);
            }
            cur++;
        }
        swap(arr, prev, left);
        return prev;
    }

    public void quicksort2(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        quick2(arr, left, right);
    }

    public void quick2(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        if (right - left + 1 <= 10) {

        }

        int mid = midThree(arr, left, right);
        swap(arr, mid, left);

        int tmp = partition(arr, left, right);
        quick(arr, left, tmp - 1);
        quick(arr, tmp + 1, right);
    }

    private int midThree(int[] arr, int left, int right) {
        int mid = (left + right) / 2;
        if (arr[left] < arr[right]) {
            if (arr[mid] < arr[left]) {
                return left;
            } else if (arr[mid] > arr[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if (arr[mid] > arr[left]) {
                return left;
            } else if (arr[mid] < arr[right]) {
                return right;
            } else {
                return mid;
            }
        }
    }

    public static void instersort(int[] arr, int left, int right) {
        for (int i = left; i < right; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if (arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }

            }
            arr[j + 1] = tmp;
        }
    }

    public void quicksort3(int[] arr) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = arr.length - 1;
        int pivot = partition(arr, left, right);
        if (pivot > left + 1) {
            stack.push(left);
            stack.push(pivot - 1);
        }
        if (pivot < right - 1) {
            stack.push(pivot + 1);
            stack.push(right);
        }

        while (!stack.empty()) {
            left = stack.pop();
            right = stack.pop();
            pivot = partition(arr, left, right);
            if (pivot > left + 1) {
                stack.push(left);
                stack.push(pivot - 1);
            }
            if (pivot < right - 1) {
                stack.push(pivot + 1);
                stack.push(right);
            }
        }
    }

    public void mergeSort1(int[] array) {
        mergeSortFunc(array, 0, array.length - 1);
    }

    private void mergeSortFunc(int[] array, int left, int right) {
        if (left >= right) {
            return;
        }

        int mid = (left + right) / 2;
        mergeSortFunc(array, left, mid);
        mergeSortFunc(array, mid + 1, right);
        merge(array, left, right, mid);
    }

    private static void merge(int[] array, int left, int mid, int right) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        int[] arr = new int[right - left + 1];
        int k = 0;
        while (s1 <= e1 && s2 <= e2) {
            if (array[s1] > array[s2]) {
                arr[k++] = array[s2++];
            } else {
                arr[k++] = array[s1++];
            }
        }
        while (s1 <= e1) {
            arr[k++] = array[s1++];
        }
        while (s2 <= e2) {
            arr[k++] = array[s2++];
        }
        for (int i = 0; i < arr.length; i++) {
            array[left + i] = arr[i];
        }
    }

    public static void mergeSort(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            // i += gap * 2 当前gap组的时候，去排序下一组
            for (int i = 0; i < array.length; i += gap * 2) {
                int left = i;
                int mid = left + gap - 1;//有可能会越界
                if (mid >= array.length) {
                    mid = array.length - 1;
                }
                int right = mid + gap;//有可能会越界
                if (right >= array.length) {
                    right = array.length - 1;
                }
                merge(array, left, mid, right);
            }
            //当前为2组有序  下次变成4组有序
            gap *= 2;
        }
    }

    public static void main(String[] args) {
        int[] arr = {3, 1, 6, 7, 4};
        mergeSort(arr);
        System.out.println(Arrays.toString(arr));
    }


    // 计数排序
    public static void countSort(int[] array) {
        int max = array[0];
        int min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
            if (array[i] > max) {
                max = array[i];
            }
        }
        int len=max-min+1;
        int[] arr = new int[len];
        for (int i = 0; i < array.length; i++) {
            arr[array[i]-min]++;
        }
        int index=0;
        for (int i = 0; i < arr.length; i++) {
            while(arr[i]>0){
                array[index++]=i+min;
                arr[i]--;
            }

        }
    }
}
